//
//  DetailedViewController.m
//  AppYing
//
//  Created by appying_wxj on 14/11/3.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "DetailedViewController.h"
#import "CommentViewController.h"
#import "UMSocialWechatHandler.h"// 微信
#import "UMSocialQQHandler.h"// qq
#import "UMSocial.h"

@interface DetailedViewController ()

@end

@implementation DetailedViewController
@synthesize detailedWebView;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.userInteractionEnabled = YES;
//    self.view.backgroundColor = [UIColor clearColor];
    // Do any additional setup after loading the view.
    [self initUI];
}
-(void)initUI{
    
    self.titleLab.text = @"具体内容";
    

    
    
    
    // 底部的view
    _footView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreen_Height-45, kScreen_Width, 45)];
    UIImageView *BgImage =[[UIImageView alloc]initWithFrame:_footView.bounds];
    BgImage.image = [UIImage imageNamed:@"image_footView@2x"];
    BgImage.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    [_footView addSubview:BgImage];
    [self.view addSubview:_footView];
    
    // 返回的按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.backgroundColor = [UIColor clearColor];
    backButton.frame = CGRectMake(0, 0, 45, 45);
    [backButton addTarget:self action:@selector(backMainView:) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:backButton];
    
    // 分享的按钮
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.backgroundColor = [UIColor clearColor];
    shareButton.frame = CGRectMake(kScreen_Width-45, 0, 45, 45);
    [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:shareButton];
    
    // 收藏的按钮
    _collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _collectButton.backgroundColor = [UIColor clearColor];
    _collectButton.frame = CGRectMake(kScreen_Width-45-13-45, 0, 45, 45);
    [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_CollectionNo@2x"] forState:UIControlStateNormal];
    [_collectButton addTarget:self action:@selector(collectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:_collectButton];
    
    
    // 信息按钮
    UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    messageButton.backgroundColor = [UIColor clearColor];
    messageButton.frame = CGRectMake(160, 0, 45, 45);
    [messageButton addTarget:self action:@selector(messageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:messageButton];
    
    
    // 撰写评论的输入框
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(51, 7, 104, 31)];
    _textField.placeholder = @"    我来说两句";
    _textField.delegate = self;
    _textField.font = [UIFont fontWithName:nil size:12];
    _textField.clearsOnBeginEditing = YES;// 再次编辑是就清空内容
    _textField.textAlignment = NSTextAlignmentLeft;// 内容的对齐方式
    _textField.borderStyle = UITextBorderStyleNone;// 输入框的样式
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;// 编辑是出现差号
    _textField.backgroundColor = [UIColor whiteColor];// 输入框的背景颜色
    _textField.returnKeyType =UIReturnKeySend;// return按键变成send
    [_footView addSubview:_textField];
    
    
    
    //webview显示评论的内容
    self.detailedWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreen_Width, kScreen_Height-64-45)];
    self.detailedWebView.opaque = NO; //不设置这个值 页面背景始终是白色
    self.detailedWebView.backgroundColor = [UIColor clearColor];
    self.detailedWebView.scalesPageToFit = NO;  //禁止用户缩放页面、
    self.detailedWebView.dataDetectorTypes = UIDataDetectorTypePhoneNumber|UIDataDetectorTypeLink;
    self.detailedWebView.scrollView.pagingEnabled = YES;
    [self.detailedWebView setScalesPageToFit:YES];//自动缩放页面以适应屏幕   yes:根据webview自适应，NO：根据内容自适应
    [self.detailedWebView setUserInteractionEnabled:NO];
    self.detailedWebView.delegate = self;
    self.detailedWebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0,0, 0);
    self.detailedWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    //[(UIScrollView *)[[self.detailedWebView subviews] objectAtIndex:0] setBounces:NO];
    //[self.detailedWebView loadHTMLString:nil baseURL:nil];
    
//     NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",16.0,[color webColorString]];
//     [self.detailedWebView stringByEvaluatingJavaScriptFromString:jsString];
    [self.view addSubview:self.detailedWebView];
    

    // 标题和内容和的分割线
    self.line = [[UIView alloc] initWithFrame:CGRectMake(10, 60, kScreen_Width-20, 0.5)];
    self.line.backgroundColor = [UIColor lightGrayColor];
    [self.detailedWebView addSubview:self.line];
    
    
}
#pragma mark~~~~~~~~~~~UIWebViewDelegate~~~~~~~~~~
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];
    //字体颜色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    //页面背景色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];

}

#pragma mark~~~~~~~~~~~UITextField delegate~~~~~~~~~
//开始编辑时触发，文本字段将成为first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    //注册通知,监听键盘弹出事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShows:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    
    //注册通知,监听键盘消失事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    
}
#pragma mark-- 键盘监听事件
// 键盘弹出时
-(void)keyboardDidShows:(NSNotification *)notification{
    //获取键盘高度
    NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect;
    
    [keyboardObject getValue:&keyboardRect];
    
    //调整放置有textView的view的位置
    //设置动画
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    // 设置撰写评论的view 向上移动
    int autoHeight = kScreen_Height -45 - keyboardRect.size.height;//高度
    _footView.frame = CGRectMake(0, autoHeight, kScreen_Width, 45);
    [UIView commitAnimations];
}
//键盘消失时
-(void)keyboardDidHidden{
    _footView.frame = CGRectMake(0, kScreen_Height-45, kScreen_Width, 45);
}
#pragma mark~~~~~~~~~~~~~~~~~~~ButtonClick~~~~~~~~~~~~~~~~~~~
// 返回的按钮的点击事件
-(void)backMainView:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:NO completion:nil];
}
// 收藏按钮的点击事件
-(void)collectButtonClick:(UIButton *)sender{
    
    if (!collect) {// 已收藏
        [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_ CollectionFinish@2x"] forState:UIControlStateNormal];
        
    }else{// 取消收藏
        [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_CollectionNo@2x"] forState:UIControlStateNormal];
    }
    collect =!collect;
}
// 分享按钮的点击事件
-(void)shareButtonClick:(UIButton *)sender{
    //状态提示框
    _greyView = [[UIView alloc]initWithFrame:
                 CGRectMake(0, 0, Kwidth, kheight)];
    _greyView.backgroundColor =
    [UIColor colorWithRed:70.0 / 255.0 green:70.0 / 255.0 blue:70.0 / 255.0 alpha:0.5];
    [self.view addSubview:_greyView];
    
    UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(10, kheight, 300, 100)];
    alertView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.8];
    [alertView.layer setMasksToBounds:YES];
    [alertView.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [alertView.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0 });
    [alertView.layer setBorderColor:colorref];//边框颜色
    
    //qq按钮
    UIButton *qqButton = [UIButton buttonWithType:UIButtonTypeCustom];
    qqButton.frame = CGRectMake(35, 30, 40, 40);
    [qqButton setImage:[UIImage imageNamed:@"image_QQ.png"] forState:UIControlStateNormal];
    [qqButton addTarget:self action:@selector(qqButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    qqButton.tag = 100;
    [alertView addSubview:qqButton];
    
    //微信按钮
    UIButton *weiXinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weiXinButton.frame = CGRectMake(100, 30, 40, 40);
    [weiXinButton setImage:[UIImage imageNamed:@"sns_icon_22.png"] forState:UIControlStateNormal];
    [weiXinButton addTarget:self action:@selector(weiXinButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    weiXinButton.tag = 101;
    [alertView addSubview:weiXinButton];
    
    //  取消的按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(10, kheight+120, 300, 40);
    [cancelButton.layer setMasksToBounds:YES];
    [cancelButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [cancelButton.layer setBorderWidth:1.0];
    [cancelButton.layer setBorderColor:colorref];//边框颜色
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.8];
    [cancelButton addTarget:self action:@selector(backViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [UIView animateWithDuration:0.5 animations:^{
        alertView.frame=CGRectMake(10, kheight-170, 300, 100);
        cancelButton.frame = CGRectMake(10, kheight-60, 300, 40);
        [_greyView addSubview:cancelButton];
        [_greyView addSubview:alertView];
    }completion:^(BOOL finished) {
    }];
    
}

// 信息按钮的点击事件
-(void)messageButtonClick:(UIButton *)sender{
    CommentViewController *commentView = [[CommentViewController alloc] init];
    commentView.positionStrings = self.positionStrings;
    [self presentViewController:commentView animated:NO completion:nil];
    
}

#pragma mark-- 分享按钮界面的点击事件
// 分享界面的取消按钮
-(void)backViewController:(UIButton *)sender{
    [_greyView removeFromSuperview];
}
// 点击QQ按钮
-(void)qqButtonClick:(UIButton *)sender{
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"分享文字" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
        }
    }];
    
}
//点击微信上的按钮
-(void)weiXinButtonClick:(UIButton *)sender{
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"分享内嵌文字" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
        }
    }];
    
}
// 键盘消失的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
